package durose.durose;

import java.util.List;
import java.util.regex.Matcher;

import durose.system.pojo.DuRose;

public class DuRoseGeneratorJspAdd {
	public static  String initInputAdd(List<DuRose> duroses){
		StringBuffer inputs = new StringBuffer();
		for(DuRose durose:duroses){
			inputs.append(initInputAdd(durose)+"\n");
		}
		return inputs.toString();
	}
	
	//根据输入的rose数据转换成对应的<durose>标签
	public static  String initInputAdd(DuRose durose){
		String inputStr = "<durose $id$ $name$ $type$ $value$  $columnExtraValue$ $columnForeignValue$></durose>";
		inputStr = inputStr.replace("$id$", "id='"+durose.getColumnName()+"'");
		inputStr = inputStr.replace("$name$", "name='"+durose.getColumnChinaName()+"'");
		inputStr = inputStr.replace("$type$", "type='"+durose.getColumnType()+"'");
		inputStr = inputStr.replace("$value$", "value='"+(durose.getColumnDefaultValue()==null?"":durose.getColumnDefaultValue())+"'");
		//如果没有表关联
		if(durose.getColumnRelaTable()==null||durose.getColumnRelaTable()==""){
			inputStr = inputStr.replace("$columnForeignValue$", "");
			//如果待选值为空
			if(durose.getColumnExtraValue()==null||durose.getColumnExtraValue()==""){
				inputStr = inputStr.replace("$columnExtraValue$", "");
			}else{
				inputStr = inputStr.replace("$columnExtraValue$", "extraValue='"+durose.getColumnExtraValue()+"'");
			}
		}else{//如果有表关联
			inputStr = inputStr.replace("$columnForeignValue$", "foreginValue='"+"<c:forEach items='${"+durose.getColumnRelaTable()+"s}' var='"+durose.getColumnRelaTable()+"'>${"+durose.getColumnRelaTable()+"."+durose.getColumnForeignValue()+"}$</c:forEach>'");
			inputStr = inputStr.replace("$columnExtraValue$", "extraValue='"+"<c:forEach items='${"+durose.getColumnRelaTable()+"s}' var='"+durose.getColumnRelaTable()+"'>${"+durose.getColumnRelaTable()+"."+durose.getColumnExtraValue()+"}$</c:forEach>'");
		}
		return inputStr;
	}
}
